package com.ruoyi.rms.controller;

import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.rms.domain.DishesOrder;
import com.ruoyi.rms.service.IDishesOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 点餐信息Controller
 * 
 * @author wyl
 * @date 2024-03-11
 */
@RestController
@RequestMapping("/rms/dishesOrder")
public class DishesOrderController extends BaseController
{
    @Autowired
    private IDishesOrderService dishesOrderService;

    /**
     * 查询点餐信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rms:dishesOrder:list')")
    @GetMapping("/list")
    public TableDataInfo list(DishesOrder dishesOrder)
    {
        startPage();
        List<DishesOrder> list = dishesOrderService.selectDishesOrderList(dishesOrder);
        return getDataTable(list);
    }

    /**
     * 根据订单信息查询点餐情况
     */
    @GetMapping("/selectDishesByOrderId/{orderId}")
    public AjaxResult selectDishesByOrderId(@PathVariable("orderId") Long orderId) {
        return AjaxResult.success(dishesOrderService.selectDishesByOrderId(orderId));
    }

    /**
     * 导出点餐信息列表
     */
//    @PreAuthorize("@ss.hasPermi('rms:dishesOrder:export')")
    @Log(title = "点餐信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DishesOrder dishesOrder)
    {
        List<DishesOrder> list = dishesOrderService.selectDishesOrderList(dishesOrder);
        ExcelUtil<DishesOrder> util = new ExcelUtil<DishesOrder>(DishesOrder.class);
        util.exportExcel(response, list, "点餐信息数据");
    }

    /**
     * 获取点餐信息详细信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:dishesOrder:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") Long orderId)
    {
        return success(dishesOrderService.selectDishesOrderByOrderId(orderId));
    }

    /**
     * 新增点餐信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:dishesOrder:add')")
//    @Log(title = "点餐信息", businessType = BusinessType.INSERT)
    @PostMapping("/{orderId}")
    public AjaxResult add(@PathVariable("orderId") Long orderId, @RequestBody Map<Long, Integer> dishesOrderMap)
    {
        return dishesOrderService.insertDishesOrder(orderId, dishesOrderMap) > 0 ? AjaxResult.success("下单成功") : AjaxResult.error("此订单已付款或已被终止，无法点餐");
    }

    /**
     * 修改点餐信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:dishesOrder:edit')")
    @Log(title = "点餐信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DishesOrder dishesOrder)
    {
        return toAjax(dishesOrderService.updateDishesOrder(dishesOrder));
    }

    /**
     * 删除点餐信息
     */
//    @PreAuthorize("@ss.hasPermi('rms:dishesOrder:remove')")
    @Log(title = "点餐信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{orderIds}")
    public AjaxResult remove(@PathVariable Long[] orderIds)
    {
        return toAjax(dishesOrderService.deleteDishesOrderByOrderIds(orderIds));
    }
}
